package com.ss.spider.system.role.controller;

import com.ss.annotation.OpLog;
import com.ss.controller.AbstractController;
import com.ss.enums.OperaTypeEnum;
import com.ss.enums.StatusEnum;
import com.ss.enums.SystemInitFlagEnum;
import com.ss.response.PageEntity;
import com.ss.response.ResponseEntity;
import com.ss.spider.system.role.form.RoleForm;
import com.ss.spider.system.role.form.RoleQuery;
import com.ss.spider.system.role.model.Role;
import com.ss.spider.system.role.service.RoleService;
import com.ss.tools.ArraysUtils;
import com.ss.tools.DateUtils;
import com.github.pagehelper.Page;

import java.util.List;

import org.mybatis.spring.MyBatisSystemException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping({"/role"})
public class RoleController extends AbstractController {

    @Autowired
    @Qualifier("roleService")
    private RoleService<Role> roleService;

    @RequestMapping(value = {"/pages"}, method = {RequestMethod.POST})
    @OpLog(model = "70006", desc = "分页查询角色列表", type = OperaTypeEnum.SELECT)
    public ResponseEntity<PageEntity<Role>> pages(@RequestBody RoleQuery para) {
        int currPage = getPageIndex(para);
        int pageSize = getPageSize(para);

        Role role = new Role();
        BeanUtils.copyProperties(para, role);

        Page<Role> data = (Page) this.roleService.pages(role, currPage, pageSize);

        ResponseEntity<PageEntity<Role>> resp = createSuccResponse();
        resp.setData(new PageEntity(data));

        return resp;
    }


    @RequestMapping(value = {"/list"}, method = {RequestMethod.POST})
    public ResponseEntity<List<Role>> list(@RequestBody RoleQuery para) {
        Role role = new Role();
        BeanUtils.copyProperties(para, role);
        List<Role> data = this.roleService.list(role);
        ResponseEntity<List<Role>> resp = createSuccResponse();
        resp.setData(data);
        return resp;
    }


    @RequestMapping(value = {"/get"}, method = {RequestMethod.POST})
    @OpLog(model = "70006", desc = "获取角色信息", type = OperaTypeEnum.SELECT)
    public ResponseEntity<Role> get(@RequestBody @Validated({com.ss.valide.APIGetsGroup.class}) RoleQuery para, BindingResult bindingResult) throws BindException {
        ResponseEntity<Role> resp = validite(bindingResult);

        resp.setData(this.roleService.get(para.getRoleId()));

        return resp;
    }


    @RequestMapping(value = {"/save"}, method = {RequestMethod.POST})
    @OpLog(model = "70006", desc = "新增角色信息", type = OperaTypeEnum.ADD)
    public ResponseEntity<String> save(@RequestBody @Validated({com.ss.valide.APIAddGroup.class}) RoleForm para, BindingResult bindingResult) throws BindException {
        ResponseEntity<String> resp = validite(bindingResult);

        Role role = new Role();
        BeanUtils.copyProperties(para, role);
        if (role.getStatus() == null) {
            role.setStatus(Integer.valueOf(StatusEnum.EFFECT.getCode()));
        }
        role.setInitFlag(Integer.valueOf(SystemInitFlagEnum.NO.getCode()));
        role.setCreatedTime(Long.valueOf(DateUtils.getCurrentTime()));
        role.setUpdatedTime(Long.valueOf(DateUtils.getCurrentTime()));

        try {
            resp.setData(this.roleService.save(role));
        } catch (MyBatisSystemException e) {
            this.logger.error("新增角色基本信息失败,原因：", e);
            throw e;
        } catch (DataAccessResourceFailureException e) {
            this.logger.error("新增角色基本信息失败,原因：", e);
            throw e;
        } catch (Exception e) {
            this.logger.error("新增角色基本信息失败,原因：", e);
            resp = createFailResponse();
            resp.setMessage(e.getMessage());

            return resp;
        }

        return resp;
    }


    @RequestMapping(value = {"/edit"}, method = {RequestMethod.POST})
    @OpLog(model = "70006", desc = "修改角色信息", type = OperaTypeEnum.EDIT)
    public ResponseEntity<String> edit(@RequestBody @Validated({com.ss.valide.APIEditGroup.class}) RoleForm para, BindingResult bindingResult) throws BindException {
        ResponseEntity<String> resp = validite(bindingResult);

        Role role = new Role();
        BeanUtils.copyProperties(para, role);

        role.setUpdatedTime(Long.valueOf(DateUtils.getCurrentTime()));

        try {
            this.roleService.update(role);
        } catch (Exception e) {
            this.logger.error("编辑角色基本信息失败,原因：", e);
            resp = createFailResponse();
            resp.setMessage(e.getMessage());

            return resp;
        }

        return resp;
    }


    @RequestMapping(value = {"/opStatus"}, method = {RequestMethod.POST})
    @OpLog(model = "70006", desc = "切换角色状态", type = OperaTypeEnum.EDIT)
    public ResponseEntity<String> opStatus(@RequestBody @Validated({com.ss.valide.APIOpStatusGroup.class}) RoleForm para, BindingResult bindingResult) throws BindException {
        ResponseEntity<String> resp = validite(bindingResult);
        List<String> roleIds = ArraysUtils.asList(para.getRoleIds());

        try {
            this.roleService.opStatus(roleIds, para.getStatus());
        } catch (MyBatisSystemException e) {
            this.logger.error("角色启用停用失败,原因：", e);
            throw e;
        } catch (DataAccessResourceFailureException e) {
            this.logger.error("角色启用停用失败,原因：", e);
            throw e;
        } catch (Exception e) {
            this.logger.error("角色启用停用失败，原因：", e);
            resp = createFailResponse();
            resp.setMessage(e.getMessage());
        }
        return resp;
    }


    @RequestMapping(value = {"/delete"}, method = {RequestMethod.POST})
    @OpLog(model = "70006", desc = "删除角色信息", type = OperaTypeEnum.DELETE)
    public ResponseEntity<String> delete(@RequestBody @Validated({com.ss.valide.APIDeltGroup.class}) RoleForm para, BindingResult bindingResult) throws BindException {
        ResponseEntity<String> resp = validite(bindingResult);
        List<String> roleIds = ArraysUtils.asList(para.getRoleIds());


        try {
            if (para.getThorough() == 0) {
                this.roleService.discard(roleIds, para.getDeletedUserid());
            } else {
                this.roleService.delete(roleIds);
            }
        } catch (MyBatisSystemException e) {
            this.logger.error("删除角色基本信息失败,原因：", e);
            throw e;
        } catch (DataAccessResourceFailureException e) {
            this.logger.error("删除角色基本信息失败,原因：", e);
            throw e;
        } catch (Exception e) {
            this.logger.error("删除角色基本信息失败，原因：", e);
            return createFailResponse();
        }

        return resp;
    }

}
